var options = {
  watermark: 1,
  logo: 1,
  resize: 0,
  method: 'uploadfile',
}

$(function () {
  renderUploader();

  $("#it4t_watermark option").filter('[value=' + getWatermark() + ']').attr('selected', true);
  $("#it4t_resize option").filter('[value=' + getResize() + ']').attr('selected', true);

  $('input:radio[name=it4t_logo]').removeAttr("checked").filter('[value=' + getLogo() + ']').attr('checked', true);

  $(".method").hide();
  $("." + getMethod()).show();

  $('input:radio').click(function () {
    $.cookie($(this).attr('name'), $(this).val());
    self.setTimeout("window.history.go(0);", 0);
  });
  $("#it4t_watermark, #it4t_resize").change(function () {
    $.cookie($(this).attr('id'), $(this).val());
    self.setTimeout("window.history.go(0);", 0);
  });

  $("#uploadfile").click(function () {
    $(".transload").hide();
    $(".uploadfile").show();
    $.cookie('method', 'uploadfile');
	$("#liup").addClass('active');
	$("#litr").removeClass('active');
    clearlist();
  });

  $("#transload").click(function () {
    $(".uploadfile").hide();
    $(".transload").show();
    $(".links").html('- Nhập link ảnh vào để transload\n- Mỗi link ảnh 1 dòng\n- Có hỗ trợ link ảnh trong thẻ [IMG]');
    $.cookie('method', 'transload');
	$("#litr").addClass('active');
	$("#liup").removeClass('active');
    clearlisttran();
  });

  $(".transload").click(function () {
    $(".links").html('');

  });

  $("#RestartTran").click(function () {
    $(".transload .links").show();
    $("#list").hide();
  });
  $(".transload #transload-button").click(function () {
    transload();

  });

});

function getWatermark() {
  return $.cookie('it4t_watermark') == null ? options.watermark : $.cookie('it4t_watermark');
}

function getLogo() {
  return $.cookie('it4t_logo') == null ? options.logo : $.cookie('it4t_logo');
}

function getResize() {
  return $.cookie('it4t_resize') == null ? options.resize : $.cookie('it4t_resize');
}

function getMethod() {
  return $.cookie('method') == null ? options.method : $.cookie('method');
}

function clearlist() {
  $("#fileQueue").html('');
  directlink = '';
  bbcode = '';
}


function displaypic(name, url) {

  $(".transload .links").hide();
  $(".transload .links").val('');
  $("#list").show();
  var travetran = '';

  travetran = unescape(url);
  travetran = travetran.replace('https://', '')
  directlinktran += travetran + '\n';
  bbcodetran += '[IMG]' + travetran + '[/IMG]';
  $("#list").html('<b>BBCode:</b><br><textarea id=\"bbcodeareatran\" style=\"width:97%;height:40%;\" onclick=\"this.select()\">' + bbcodetran + '</textarea><b>Direct Link:</b><br><textarea style=\"width:97%;height:40%\" id=\"directareatran\" onclick=\"this.select()\">' + directlinktran + '</textarea>');


}
function clearlisttran() {
  $("#list").html('');
  $("#list").hide();
  $(".transload .links").show();
  directlinktran = '';
  bbcodetran = '';
}



function ShowWaiting() {
  $('#it4t_over').show();
  $('#it4t_uploader_loading').show();
}

function HideWaiting() {
  $('#it4t_over').hide();
  $('#it4t_uploader_loading').hide();
}

function getInputLinks() {

  var text = $(".transload .links").val();
  var re = /.*?(\[IMG\])?(https?[^\\\n[]+)(\[\/IMG\])?/ig;
  var m;
  var links = new Array();
  var i = 0;
  while (m = re.exec(text)) {
    links[i++] = $.trim(m[2]);
  }
  return links;
}

function doTransload(id, links) {
  var stt = id + 1;
  var url = links[id];
  $.ajax({
    data: {
      'url': url,
      'watermark': getWatermark(),
      'resize': getResize(),
      'logo': getLogo(),
    },
    url: 'uploadify.php',
    cache: false,
    type: "POST",
    success: function (msg) {
      displaypic(stt, msg)
      if (stt < links.length) {
        doTransload(stt, links);
      } else HideWaiting();
    },
    error: function () {
      alert("Có lỗi, vui lòng thử lại");
    },
  });
}

function transload() {
  clearlisttran();

  var links = getInputLinks();
  if (links.length == 0) {
    alert('Bạn cần phải nhập đường dẫn ảnh');
    return;
  }
  ShowWaiting();
  doTransload(0, links);

}


function renderUploader() {

  $("#uploadify").uploadify({
    'swf': 'scripts/uploadify.swf',
    'uploader': 'uploadify.php',
    'queueID': 'fileQueue',
    'auto': false,
    'multi': true,
    'fileExt': '*.jpg;*.gif;*.png;*.bmp;*.jpeg',
    'formData': {
      'watermark': getWatermark(),
      'logo': getLogo(),
      'resize': getResize(),
    },
    'onUploadStart': function (file) {
      ShowWaiting();
    },
    'onQueueComplete': function (queueData) {            HideWaiting();        
    }
  });
}